package com.darrenchan.spark.rdd

import org.apache.spark.{SparkConf, SparkContext}

/**
  * 计算平均年龄
  */
object AvgAge {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("AvgAge")

    val sc = new SparkContext(sparkConf)

    val rdd = sc.textFile("age.txt").map(_.split(" ")(1))

    val ageSum = rdd.map(_.toInt).reduce(_+_)

    val count = rdd.count()

    println(ageSum * 1.0 / count)

    sc.stop()
  }
}
